'''
对五分类数据集（腺癌，鳞癌，腺鳞癌，大细胞癌，小细胞癌）进行分布统计
绘制统计盒图
针对性别特征，绘制统计直方图
'''

import matplotlib.pyplot as plt
import pandas as pd
import os

# 读取统计数据
def read_data():
    csv_path = 'D:/lung_cancer/data/all_data2.csv'
    data = pd.read_csv(csv_path)
    return data

# 将某类数据按类别划分
def devide_data(data, cancer_type):

    x_list = [[],[],[],[],[]]
    for i in range(len(data)):
        x_list[cancer_type[i]-1].append(float(data[i]))

    return x_list


def draw_box(one_data, name):

    print('------start draw ', name, '------')
    plt.figure(figsize=(8, 6))
    plt.boxplot(one_data, notch=False, sym='rs', vert=True, showmeans=True)
    plt.xticks([y + 1 for y in range(5)], ['1', '2', '3', '4', '5'])
    plt.xlabel('measurement '+name)
    plt.title('Box plot')

    plt.savefig(name)
    plt.show()
    plt.close()


# 对性别做统计，直方图更合适
def draw_hist(one_data, name):
    one_men = 0
    one_women = 0
    two_men = 0
    two_women = 0
    three_men = 0
    three_women = 0
    four_men = 0
    four_women = 0
    five_men = 0
    five_women = 0
    one_list = one_data[0]
    two_list = one_data[1]
    three_list = one_data[2]
    four_list = one_data[3]
    five_list = one_data[4]

    for item1 in one_list:
        if int(item1) == 0:
            one_men = one_men+1
        else:
            one_women = one_women+1

    for item2 in two_list:
        if int(item2) == 0:
            two_men = two_men+1
        else:
            two_women = two_women+1

    for item3 in three_list:
        if int(item3) == 0:
            three_men = three_men+1
        else:
            three_women = three_women+1

    for item4 in four_list:
        if int(item4) == 0:
            four_men = four_men+1
        else:
            four_women = four_women+1

    for item5 in five_list:
        if int(item5) == 0:
            five_men = five_men+1
        else:
            five_women = five_women+1

    name_list = ['1', '2', '3', '4', '5']
    x = list(range(len(name_list)))
    total_width, n = 0.8, 2
    width = total_width/n
    plt.bar(x, [one_men, two_men, three_men, four_men, five_men], width=width, label='men', fc='b')
    for i in range(len(x)):
        x[i] += width
    plt.bar(x, [one_women, two_women, three_women, four_women, five_women], width=width, label='women', tick_label=name_list, fc='g')
    plt.legend()
    plt.savefig(name)
    plt.show()
    plt.close()


# 对不同特征做统计
def statistic():
    data = read_data()
    cancer_type = data['cancer_type']

    save_path = 'D:/lung_cancer/lung_cancer_code/release_code/output/five/Boxplot'
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # # 统计病灶半径
    # cancer_r = data['r']
    # r_list = devide_data(cancer_r, cancer_type)
    # draw_box(r_list, save_path+'/r')
    #
    # # suv_max
    # suvmax = data['part_suvmax']
    # suvmax_list = devide_data(suvmax, cancer_type)
    # draw_box(suvmax_list, save_path+'/suv_max')
    #
    # # suv_min
    # suvmin = data['suv_min']
    # suvmin_list = devide_data(suvmin, cancer_type)
    # draw_box(suvmin_list, save_path+'/suv_min')
    #
    # # suv_avg
    # suvavg = data['suv_avg']
    # suvavg_list = devide_data(suvavg, cancer_type)
    # draw_box(suvavg_list, save_path+'/suv_avg')
    #
    # # suv_std
    # suvstd = data['suv_std']
    # suvstd_list = devide_data(suvstd, cancer_type)
    # draw_box(suvstd_list, save_path+'/suv_std')
    #
    # # age
    # age = data['patientAge']
    # age_list = devide_data(age, cancer_type)
    # draw_box(age_list, save_path+'/age')

    # size
    size = data['patientSize']
    size_list = devide_data(size, cancer_type)
    draw_box(size_list, save_path + '/Size')

    # # weight
    # patientWeight = data['patientWeight']
    # patientWeight_list = devide_data(patientWeight, cancer_type)
    # draw_box(patientWeight_list, save_path+'/patientWeight')
    #
    #
    # # Sex,性别用直方图统计更好
    # patientSex = data['patientSex']
    # patientSex_list = devide_data(patientSex, cancer_type)
    # draw_hist(patientSex_list, save_path + '/patientSex')


if __name__ == '__main__':

    statistic()